import {_decorator, Component, Node, tween, UITransform, v3, Vec3} from 'cc';
import {MoneyAnim} from "./MoneyAnim";
import {AudioClipID, AudioManager} from "./AudioManager";
import {GameMain} from "./GameMain";
import DailyAwardData from "./DailyAwardData";
import Platform, {PlatformType} from "./Platform";
import {NativeEvt} from "./GameConfig";

const { ccclass, property } = _decorator;

@ccclass('DailyAwardUI')
export class DailyAwardUI extends Component {

    /*
    DailyAwardUI显示规则
    存储的lastAutoShowTime和当前时间如果不是同一天,只自动弹出一次
     */

    @property({type:Node})
    targetNode:Node=null;

    @property({type:MoneyAnim})
    moneyAnim:MoneyAnim=null;

    public dayNodes:Array<Node>=[];
    public lightNodes:Array<Node>=[];
    public selUITransform:UITransform=null;

    public getAwardBtnNode:Node=null;
    public doubleAwardBtnNode:Node=null;
    public alreadyLabelNode:Node=null;

    public awardDay:number=-1;
    public canGetAward:boolean=true;

    public pageClosed:boolean=false;

    public static readonly awardNum:Array<number>=[100, 130, 160, 190, 220];

    onLoad()
    {
        let parentNode:Node=this.targetNode.getChildByName("parent");
        this.selUITransform=parentNode.getChildByName("selFrame").getComponent(UITransform);
        this.getAwardBtnNode=this.targetNode.getChildByName("getBtn");
        this.doubleAwardBtnNode=this.targetNode.getChildByName("doubleBtn");
        this.alreadyLabelNode=this.targetNode.getChildByName("alreadyLabel");

        let nodeName:string="";
        for(let i=0;i<5;i++)
        {
            nodeName="day"+i;
            this.dayNodes.push(parentNode.getChildByName(nodeName));
            this.lightNodes.push(this.dayNodes[i].getChildByName("bg").getChildByName("light"));
        }

    }

    start() {

    }

    public onGetClicked()
    {
        if(this.pageClosed)
            return;
        this.pageClosed=true;

        this.moneyAnim.setDstNode(GameMain.instance.startGameUI.moneySpriteNode);
        this.moneyAnim.play(()=>{

        });
        this.scheduleOnce(()=>{
            AudioManager.playClip(AudioClipID.Coin);
        }, 0.3);
        //获取钻石
        GameMain.instance.gameData.addMoney(DailyAwardUI.awardNum[this.awardDay]);
        GameMain.instance.startGameUI.updateMoneyLabel();
        this.scheduleOnce(()=>{
            this.close();
        }, 0.8);

        //更新领取时间
        GameMain.instance.dailyAwardData.updateLastGetAwardTime(this.awardDay);
        this.scheduleOnce(()=>{
            GameMain.instance.startGameUI.updateRedPoint();
        }, 0.2);
    }

    public onDoubleClicked()
    {
        GameMain.instance.setVedioAwardScene(NativeEvt.VedioDaily);
        Platform.playVideoAd(() =>
        {
            this.videoAward();
        });
    }

    public videoAward()
    {
        this.moneyAnim.setDstNode(GameMain.instance.startGameUI.moneySpriteNode);
        this.moneyAnim.play(()=>{

        });
        this.scheduleOnce(()=>{
            AudioManager.playClip(AudioClipID.Coin);
        }, 0.3);
        //获取钻石
        GameMain.instance.gameData.addMoney(DailyAwardUI.awardNum[this.awardDay] * 2);
        GameMain.instance.startGameUI.updateMoneyLabel();
        this.scheduleOnce(()=>{
            this.close();
        }, 0.8);

        //更新领取时间
        GameMain.instance.dailyAwardData.updateLastGetAwardTime(this.awardDay);
        this.scheduleOnce(()=>{
            GameMain.instance.startGameUI.updateRedPoint();
        }, 0.2);
    }

    public checkAutoShow()
    {

        let canGetAward:boolean=this.checkAwardTime();
        if(canGetAward)
        {
            this.show();
        }

        /*
        if(GameMain.instance.dailyAwardData.lastAutoShowTime<0)
        {
            this.show();
            //保存最后自动显示时间
            GameMain.instance.dailyAwardData.updateLastAutoShowTime();
            return;
        }
        //记录的最后自动显示时间,和当前时间不是同一天,则应自动显示
        let now=new Date();
        let lastShowDate=new Date(GameMain.instance.dailyAwardData.lastAutoShowTime);
        if(now.getFullYear()===lastShowDate.getFullYear() && now.getMonth()===lastShowDate.getMonth() && now.getDate()===lastShowDate.getDate())
        {
            //同一天不显示
            console.log("dailyAwardUI 同一天不自动显示 month="+lastShowDate.getMonth()+"  day="+lastShowDate.getDate());
        }
        else
        {
            this.show();
            //保存最后自动显示时间
            GameMain.instance.dailyAwardData.updateLastAutoShowTime();
        }*/
    }

    //计算是第几天领取, 返回是否可领取
    public checkAwardTime():boolean
    {
        this.awardDay=0;
        this.canGetAward=true;
        let lastGetAwardTime:number=GameMain.instance.dailyAwardData.lastGetAwardTime;

        if(lastGetAwardTime<0)
        {
            //从第0天开始
            this.awardDay=0;
        }
        else
        {
            //计算时间差值
            let lastDate=new Date(lastGetAwardTime);
            //设置到当天23:59
            lastDate.setHours(23);
            lastDate.setMinutes(59);
            lastDate.setSeconds(59);
            let nextDayTime:number=lastDate.getTime() + 24*60*60*1000;

            let nowTime:number=Date.now();
            if(nowTime>lastDate.getTime()+ 10*1000 && nowTime<=nextDayTime)
            {
                //符合连续领取条件
                this.awardDay=GameMain.instance.dailyAwardData.dayCount+1;
                if(this.awardDay>=DailyAwardData.maxDayCount)
                    this.awardDay=0;

            }
            else if(nowTime>nextDayTime)
            {
                //重置
                this.awardDay=0;
            }
            else if(nowTime>lastGetAwardTime && nowTime<=lastDate.getTime())
            {
                //当天已经领取的状态
                this.awardDay=GameMain.instance.dailyAwardData.dayCount;
                this.canGetAward=false;
            }
        }

        return this.canGetAward;
    }

    public show()
    {
        if(this.node.active)
            return;
        this.pageClosed=false;
        this.node.active=true;
        this.checkAwardTime();
        this.targetNode.setScale(0.4, 0.4,0.4);

        this.updateUI();
        tween(this.targetNode)
            .to(0.3,{scale:Vec3.ONE}, { easing: 'backOut' })
            .call(()=>{
                if(Platform.isOV() && !this.canGetAward)
                {
                    Platform.createOVCustomAd();
                }
                else if(Platform.isAndroid() && !this.canGetAward)
                {
                    GameMain.instance.showNativeAd();
                }
                else if(Platform.currentPlatform===PlatformType.WECHAT && !this.canGetAward)
                {
                    Platform.createInsertAd();
                }
                else if(Platform.currentPlatform===PlatformType.TT && !this.canGetAward)
                {
                    Platform.createInsertAd();
                }
            })
            .start();
    }

    public updateUI()
    {
        this.getAwardBtnNode.active=true;
        this.doubleAwardBtnNode.active=true;
        this.alreadyLabelNode.active=false;
        this.selUITransform.node.active=true;
        for(let i=0;i<this.lightNodes.length;i++)
        {
            this.lightNodes[i].active=false;
        }
        this.selUITransform.node.setPosition(this.dayNodes[this.awardDay].getPosition());

        if(this.awardDay>=0 && this.awardDay<DailyAwardData.maxDayCount)
        {
            if(this.awardDay===DailyAwardData.maxDayCount-1)
            {
                this.selUITransform.setContentSize(384,182);
            }
            else
            {
                this.selUITransform.setContentSize(184,182);
            }
        }

        if(!this.canGetAward)
        {
            this.getAwardBtnNode.active=false;
            this.doubleAwardBtnNode.active=false;
            this.alreadyLabelNode.active=true;
            //this.selUITransform.node.active=false;
            return;
        }

        if(this.awardDay>=0 && this.awardDay<DailyAwardData.maxDayCount)
        {
            this.lightNodes[this.awardDay].active=true;
        }
    }


    public close()
    {
        tween(this.targetNode).to(0.2,{scale:v3(0.3, 0.3, 0.3)}).call(()=>{
            this.node.active=false;
        }).start();
    }
}

